using System;
using System.Collections.Generic;
using System.Text;
using CodePlex.Resourceful;

namespace CodePlex.SpaceBlock
{
    public class S3DefaultTransferACL
    {

        public static readonly S3DefaultTransferACL Private = new S3DefaultTransferACL(0, "private");
        public static readonly S3DefaultTransferACL PublicRead = new S3DefaultTransferACL(1, "public-read");
        public static readonly S3DefaultTransferACL Bucket = new S3DefaultTransferACL(2, "bucket");

        public static readonly IEnumerable<S3DefaultTransferACL> All = new S3DefaultTransferACL[] 
        {
            Private,PublicRead,Bucket
        };


        private readonly int _value;
        private readonly string _name;

        private S3DefaultTransferACL(int value, string name)
        {
            _value = value;
            _name = name;
        }

        public int Value { get { return _value; } }
        public string Name { get { return _name; } }

        public override bool Equals(object obj)
        {
            S3DefaultTransferACL other = obj as S3DefaultTransferACL;
            return other != null && other.Value.Equals(this.Value);
        }

        public override int GetHashCode()
        {
            return this.Value.GetHashCode();
        }

        public override string ToString()
        {
            return this.Name;
        }

        public static S3DefaultTransferACL Parse(string defaultTransferACL)
        {
            S3DefaultTransferACL rt = null;

            int intValue;
            if (int.TryParse(defaultTransferACL, out intValue))
            {
                rt = Utils.Find(All, delegate(S3DefaultTransferACL item)
                {
                    return item.Value.Equals(intValue);
                });
            }

            if (rt == null)
            {
                rt = Utils.Find(All, delegate(S3DefaultTransferACL item)
                {
                    return item.Name.Equals(defaultTransferACL);
                });
            }


            return rt??S3DefaultTransferACL.Private;
            
        }
    }
}
